"""
Case Type   : 数据库系统
Case Name   : 数据库兼容PG模式，localdatetime类型setObject插入Date类型数据
Create At   : 2021/04/20
Owner       : opentestcase026
Description :
    1.写配置文件
    2.编译java工具
    3.运行java工具
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
    modified: 2020/12/04  modified by opentestcase001:适配低版本yat
    modified: 2021/9/24  modified by opentestcase001:适配最新版本代码，修改pg_hba文件
    后需重启数据库
    modified: 2021/11/12  modified by opentestcase001:完善环境清理
    modified: 2023/03/15 modified by opentestcase004:与开发沟通，java本身存在转换问题，
    java脚本中LocalDateTime.MAX，LocalDateTime.MIN执行结果在centos和
    x86-openeluer环境会出现不一致情况，优化java脚本表字段类型& 优化用例格式
"""
import unittest
import os
import time
from datetime import date, timedelta
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Jdbcisreadonly(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info("-----------this is setup-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.targetpath = os.path.join("/home", "/jdbc_test")
        self.properties = os.path.join(self.targetpath,
                                       "jdbc_case0001.properties")
        self.sql_path = os.path.join(self.targetpath, "jdbc_set_get_object")
        self.java_name = "jdbc_set_get_object_case0004"
        self.common = Common()
        self.commsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_jdbc_1"
        self.bak_hba = os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf_t_bak')
        self.init_hba = os.path.join(macro.DB_INSTANCE_PATH, 'pg_hba.conf')

    def test_index(self):
        self.log.info('--------------1.创建数据库--------------')
        result = self.commsh.execut_db_sql(
            f"drop database if exists {self.db_name};"
            f"create database {self.db_name} DBCOMPATIBILITY='PG';")
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result)

        self.log.info('--------写配置文件-------')
        cmd = f"cp {self.init_hba} {self.bak_hba}"
        self.db_primary_user_node.sh(cmd)
        cmd = f"grep -nr '127.0.0.1/32' {self.init_hba}"
        self.log.info(cmd)
        line = self.db_primary_root_node.sh(
            cmd).result().splitlines()[0].split(':')[0]
        self.log.info(line)
        cmd = f'sed -i "{str(int(line) + 1)}ihost all all ' \
            f'{self.db_primary_user_node.db_host}/32 sha256" ' \
            f'{self.init_hba}; ' \
            f'cat {self.init_hba}'
        self.log.info(cmd)
        result = self.db_primary_user_node.sh(cmd).result()
        self.log.info(result)
        self.common.scp_file(self.db_primary_root_node,
                             f"{self.java_name}.java", self.targetpath)
        result = self.db_primary_root_node.sh(
            f"touch {self.properties}").result()
        self.log.info(result)
        config = f'echo "password=' \
            f'{self.db_primary_user_node.db_password}"> {self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'echo "port={self.db_primary_user_node.db_port}">> ' \
            f'{self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'echo "hostname={self.db_primary_user_node.db_host}">> ' \
            f'{self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'echo "user={self.db_primary_user_node.db_user}">> ' \
            f'{self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'echo "dbname={self.db_name}">> ' \
            f'{self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'echo "stringtype=unspecified">> {self.properties}'
        self.db_primary_root_node.sh(config)
        config = f'cat {self.properties}'
        result = self.db_primary_root_node.sh(config).result()
        self.assertTrue("password=" in result and "port=" in result
                        and "hostname=" in result and "user=" in result
                        and "dbname=" in result)

        self.log.info('--------------reload pg_hba文件--------------')
        result = self.commsh.reload_db_config()
        self.assertIn(self.constant.gs_ctl_reload_success,result)

        self.log.info('--------------2.编译java工具--------------')
        self.db_primary_root_node.scp_put(macro.JDBC_PATH,
                                          f"{self.targetpath}/postgresql.jar")
        cmd = f"javac -encoding utf-8 -cp " \
            f"{os.path.join(self.targetpath, 'postgresql.jar')} " \
            f"{os.path.join(self.targetpath, f'{self.java_name}.java')}"
        self.log.info(cmd)
        result = self.db_primary_root_node.sh(cmd).result()
        self.log.info(result)

        self.log.info("-------------3.运行java工具--------------")
        cmd = f"java -cp {os.path.join(self.targetpath, 'postgresql.jar')}" \
            f":{self.targetpath} " \
            f"{self.java_name} -F {self.properties}"
        self.log.info(cmd)
        result = self.db_primary_root_node.sh(cmd).result()
        self.log.info(result)
        self.assertIn('第1行结果：292278994-08-17 07:00:00.0', result)
        self.assertIn('第2行结果：292269055-12-03 07:00:00.0', result)
        self.assertIn('第3行结果：null', result)
        self.assertIn('第5行结果：1970-01-01', result)
        self.assertIn('第6行结果：2021-01-29', result)
        self.assertIn('第8行结果：2020-02-29', result)
        today = date.today().strftime("%Y-%m-%d")
        self.assertIn(f'第4行结果：{today}', result)
        self.assertIn(f'第7行结果：{today}', result)

    def tearDown(self):
        self.log.info('--------------this is tearDown-------------')
        self.log.info('--------------4.清理环境--------------')
        cmd = f"rm -rf {self.targetpath}"
        self.log.info(cmd)
        self.db_primary_root_node.sh(cmd).result()

        cmd = f"ls -al {self.bak_hba}"
        self.log.info(cmd)
        res = self.db_primary_user_node.sh(cmd).result()
        self.log.info(res)
        if self.constant.NO_FILE_MSG not in res:
            cmd = f"rm -rf {self.init_hba}; " \
                  f"mv {self.bak_hba} {self.init_hba} "
            self.log.info(cmd)
            msg = self.db_primary_user_node.sh(cmd).result()
            self.log.info(msg)

        self.log.info('--------------reload pg_hba文件--------------')
        result = self.commsh.reload_db_config()
        self.assertIn(self.constant.gs_ctl_reload_success,result)

        result = self.commsh.execut_db_sql(
            f"drop database if exists {self.db_name};")
        self.log.info(result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
